const user = {
  namespaced: true,
  state: {
    token: window.localStorage.getItem('token'),
    refresh_token: window.localStorage.getItem('refresh_token'),
    cancelTokenArr: []
  },
  mutations: {
    setToken (state, token) {
      state.token = token
      window.localStorage.setItem('token', token)
    },
    setRefreshToken (state, refresh_token) {
      state.refresh_token = refresh_token
      window.localStorage.setItem('refresh_token', refresh_token)
    },
    pushCancelToken: (state, src) => {
      // cancel 取消函数
      state.cancelTokenArr.push(src.cancelToken)
    },
    // 执行取消函数 并且清空取消函数队列
    clearCancelToken: (state) => {
      state.cancelTokenArr.forEach(item => {
        item()
      })
      state.cancelTokenArr = []
    }
  }
}

export default user
